/**
 * 生成树结构
 * @param data
 * @param parentId
 * @returns {*}
 */
function buildTree (data, parentId = 0) {
    if (!Array.isArray(data)) {
        return [];
    }
    
    // 处理没有parentId的情况，视为一级目录
    const nodes = data.map(item => {
        if (typeof item.parentId === 'undefined') {
            return {...item, parentId: 0};
        }
        return item;
    });

    return nodes
        .filter(item => item.parentId == parentId)
        .map(item => ({
            ...item,
            children: buildTree(nodes, item.id)
        }));
}

function resOk (data) {
	let ok = {
		code: 0, msg: 'ok'
	}
	if (data) {
		ok.data = data
	}
	return ok
}

function resFail (msg) {
	let fail = {
		code: -1, msg: msg
	}
}

module.exports = {
	buildTree, resOk, resFail
}
